import 'package:flutter/material.dart';

class StartNaviPage extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: FirstScreen(products: List.generate( 20, (i)=>Product('商品 $i','NO.$i'))),
    );
  }
}

class Product {
  final String name;  // 名称
  final String number;  //　编码
  Product(this.name, this.number);
}

class FirstScreen extends StatelessWidget{
  final List<Product> products;
  FirstScreen({Key key,@required this.products}):super(key:key);

  _navigateToNext(BuildContext context, int index) async{ //async是启用异步方法
    //等待
    final result = await Navigator.push(
        context,
        MaterialPageRoute(builder: (context)=> new SecondScreen(product:products[index]))
    );
    Scaffold.of(context).showSnackBar(SnackBar(content:Text('$result')));
  }

  @override
  Widget build(BuildContext context){
    return new Scaffold(
        appBar: AppBar(title:Text('第1页')),
        body:ListView.builder(
          itemCount:products.length,
          itemBuilder: (context, index){
            return ListTile(
                title:Text(products[index].name),
                onTap:(){
                  _navigateToNext(context, index);
                }
            );
          },
        ));
  }
}

class SecondScreen extends StatelessWidget{
  final Product product;
  SecondScreen({Key key ,@required this.product}):super(key:key);

  @override
  Widget build(BuildContext context){
    return Scaffold(
        appBar:AppBar(title:Text('第2页')),
        body:Center(
            child:Column(
              children: <Widget>[
                Text('${product.number}'),
                RaisedButton(
                  child: Text('卖出'),
                  onPressed: (){
                    Navigator.pop(context,' ${product.name} 卖出 -1');
                  },
                ) ,
                RaisedButton(
                  child: Text('买入'),
                  onPressed: (){
                    Navigator.pop(context,' ${product.name} 买入 +1');
                  },
                ) ,
              ],
            )
        )
    );
  }
}